package com.cskaoyan.service.impl;

import com.cskaoyan.bean.FootprintParameter;
import com.cskaoyan.bean.PageData;
import com.cskaoyan.mapper.MarketFootprintMapper;
import com.cskaoyan.model.MarketFootprint;
import com.cskaoyan.model.MarketFootprintExample;
import com.cskaoyan.service.FootprintService;
import com.cskaoyan.utils.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class FootprintServiceImpl implements FootprintService {
    @Override
    public PageData list(FootprintParameter footprintParameter) {
        SqlSession sqlsession = MyBatisUtil.getSqlSession();
        MarketFootprintMapper mapper = sqlsession.getMapper(MarketFootprintMapper.class);
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        MarketFootprintExample.Criteria criteria = marketFootprintExample.createCriteria();

        criteria.andDeletedEqualTo(false);
        //分页
        Integer page = footprintParameter.getPage();
        Integer limit = footprintParameter.getLimit();
        PageHelper.startPage(page, limit);
        //排序
        String sort = footprintParameter.getSort();
        String order = footprintParameter.getOrder();
        marketFootprintExample.setOrderByClause(sort + " " + order);
        //条件
        Integer userId = footprintParameter.getUserId();
        if (userId != null && userId != 0) {
            criteria.andUserIdEqualTo(userId);
        }

        Integer goodsId = footprintParameter.getGoodsId();
        if (goodsId != null && goodsId != 0) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        List<MarketFootprint> marketFootprint = mapper.selectByExample(marketFootprintExample);

        sqlsession.commit();
        sqlsession.close();
        return PageData.list(marketFootprint);
    }
}
